$(document).ready(function () {
    getHotSearchList();
    getDomainList();
    changeJob(1);
    onNavClick(1);
})

/**
 * 获取热搜词
 */
function getHotSearchList() {
    $.ajax({
        type: "POST",
        url: "/home",
        contentType: "application/json;charset:utf-8",
        data: JSON.stringify({"param": "hot"}),
        dataType: "json",
        success: function (data) {
            if (data.code == 0) {
                createHotElement(data.data);
            } else {
                alert("获取热门搜索数据失败！");
            }
        }, error: function (message) {
            alert(message);
        }
    })
}

/**
 * 构建热门搜索Html
 */
function createHotElement(data) {
    $(data).each(function (index, bean) {
        if (index < 8) {
            var a = $("<a class='hot-tip' href='javascript:js_method();' style='margin-left: 10px'></a>").text(bean.sContent);
            $(".hot_line").append(a);
            var cond = $('.hot-tip')[index];
            $(".hot_line").find(cond).click(function () {
                onSubmitClick($(".hot_line").find(cond).text());
            });
        }
    })
}


/**
 *  获取行业列表
 */
function getDomainList() {
    loading(false);
    $.ajax({
        url: "/home",
        method: "post",
        data: JSON.stringify({"param": "domain"}),
        dataType: "json",
        success: function (data) {
            loading(true);
            if (data.code == 0) {
                createDomainElement(data.data);
            } else {
                alert("获取热门行业数据失败！");
            }
        },
        error: function (message) {
            alert(message)
            loading(true);
        }
    });
}

/**
 * 构建热门行业Html
 */
function createDomainElement(data) {
    $(data).each(function (index, domain) {
        var title = domain.domainName;
        var items = new String();
        $(domain.industries).each(function (index, industrie) {
            var industryName = industrie.industryName;
            items += "<a class='cond' href='#'>" +
                industryName +
                "</a>";
        })
        var branches = $("<li><span class='title-domain'>" +
            title +
            "</span><ul>" +
            items +
            "<div class='clear_float'></div>" +
            "</li>")
            .appendTo("#browser");
        $("#browser").treeview({
            add: branches
        });
    })
}

/**
 * 切换 -》 热门职位 和 最新职位 选中状态
 * @param index
 */
function changeJob(index) {
    var orderByHot = document.getElementById('hot-job');
    var orderByTime = document.getElementById('new-job');
    switch (index) {
        case 1://hot
            orderByHot.style.color = '#11a984';
            orderByTime.style.color = '#aaaaaa';
            changeJobData("hotJobList");
            break;
        case 2://new
            orderByHot.style.color = '#aaaaaa';
            orderByTime.style.color = '#11a984';
            changeJobData("newJobList");
            break;
    }
}

function changeJobData(param) {
    $.ajax({
        type: "POST",
        url: "/home",
        contentType: "application/json;charset:utf-8",
        data: JSON.stringify({"param": param}),
        dataType: "json",
        success: function (data) {
            if (data.code == 0) {
                createJobElement(data.data);
            } else {
                alert("获取工作列表数据失败！");
            }
        }, error: function (message) {
            alert(message);
        }
    })
}

/**
 * 工作列表*/
function createJobElement(data) {
    $("#job_list").remove();
    var job = $("<div id='job_list'></div>")
    $("body").append(job);
    $(data).each(function (index, bean) {
        var title = "<h4 style='float: left;font-weight: normal'>" + bean.jName + "</h4>";
        var time = "<h6 style='color: #bbbbbb;margin-top: 2px;float: left;margin-left: 10px'>" + "[" + fomatDate(new Date(bean.jCreateTime)) + "发布" + "]" + "</h6>";
        var message = "<img src='drawable/message.png' style='width: 18px;height: 18px;float: left;margin-top: 2px;margin-left: 10px'>"
        var wages = "<h4 style='float: right;margin-right: 20px;font-weight: normal;color: red'>" + bean.jWages + "</h4>";
        var clearFix = "<div class='clear_float'></div>";
        var desc = "<h5 style='color: #777777;font-weight: normal;margin-top: 10px;text-overflow: ellipsis;overflow: hidden;white-space: nowrap'>" + bean.jDesc + "</h5>";
        var comp = "<h6 style='font-weight: normal;margin-top: 10px'>" + bean.jCompany + "</h6>";
        var div = "<div>" + title + time + message + wages + clearFix + desc + comp + "</div>";
        var box = "<ul class='job_line'><li class='job_item'>" + div + "</li></ul>";
        var item = $(box);
        $("#job_list").append(item);
    });
}

/**
 * 隐藏显示加载框
 */
function loading(hide) {
    if (hide) {
        $("#loading").css("display", "none");
    } else {
        $("#loading").css("display", "block");
    }
}

function closeClick() {
    $("#address").toggle(500);
}

function chooseAddress(address) {
    $("#address").toggle(500);
    $("#location").text(address);
}

var type;

function onNavClick(arg1) {
    type = arg1;
    switch (type) {
        case 1:
            $("#nav-job").css({"background": "#11a984", "color": "white"});
            $("#nav-company").css({"background": "white", "color": "#777777"});
            break;
        case 2:
            $("#nav-job").css({"background": "white", "color": "#777777"});
            $("#nav-company").css({"background": "#11a984", "color": "white"});
            break;
    }
}

function onSearch() {
    onSubmitClick($("#search_box").val());
}

function onSubmitClick(text) {
    sessionStorage.setItem("searching", text);
    if (type == 1) {
        window.location = "jobPage.html";
    } else {
        window.location = "companyPage.html";
    }
}
